<########################################################################################################
	
	.AUTHOR
		Rahul G. Babar																		 
	
	.SYNOPSYS
		Automate WSP Solution Deployment
	
	.DESCRIPTION
	    Automate WSP Solution Deployment
	
	.PARAMETER SolutionPath
		Path on computer where solution WSP is kept for example c:\Solutions\MySolution.wsp.
		
	.EXAMPLE
		.\InstallSolutionWsp.ps1 SolutionPath "C:\Solutions\MySolution.wsp"  			 

    
########################################################################################################>

[CmdletBinding()]
Param(
   [Parameter(Mandatory=$true,Position=1)]
   [string]$SolutionPath
) 

function LoadSharePointPowerShellEnvironment
{	
	write-host "Setting up PowerShell environment for SharePoint" -foregroundcolor Yellow
	write-host 
	Add-PSSnapin "Microsoft.SharePoint.PowerShell" -ErrorAction SilentlyContinue
	write-host "SharePoint PowerShell Snapin loaded." -foregroundcolor Green
	write-host 
}

write-host 
LoadSharePointPowerShellEnvironment

$wspFile = Get-ChildItem $SolutionPath
$SolutionName = $wspFile.Name
$AdminServiceName = "SPAdminV4"  
$WasAdminServiceRunning = $true;   
 
if ($(Get-Service $AdminServiceName).Status -eq "Stopped")   
{   
	write-host "[[STEP]] Starting SharePoint Administration Service since it is not already started." -foregroundcolor Yellow
	write-host

    $WasAdminServiceRunning = $false;   
    Start-Service $AdminServiceName    

	write-host "[[STEP]] SharePoint Administration Service Started" -foregroundcolor Green
	write-host
} 

$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $true)}   
if ($Solution -ne $null)   
{  
	write-host "[[STEP]] Retracting solution: $SolutionName." -foregroundcolor Yellow
	write-host
	if($Solution.ContainsWebApplicationResource)   
	{   
		Uninstall-SPSolution $SolutionName -AllWebApplications -Confirm:$false   
	}   
	else   
	{   
		Uninstall-SPSolution $SolutionName -Confirm:$false   
	}   
}   
       
while ($Solution.JobExists)   
{   
	Start-Sleep 2   
} 

if(-not $Solution.Deployed)
{
	write-host "[[STEP]] Retracted $SolutionName successfully." -foregroundcolor Green
	write-host
}
	   
if ($(Get-SPSolution | ? {$_.Name -eq $SolutionName}).Deployed -eq $false)   
{   
	write-host "[[STEP]] Removing solution: $SolutionName." -foregroundcolor Yellow
	write-host
	Remove-SPSolution $SolutionName -Confirm:$false   
	write-host "[[STEP]] Removed solution $SolutionName successfully." -foregroundcolor Green
	write-host 
}    
 
write-host "[[STEP]] Adding solution: $SolutionName." -foregroundcolor Yellow
write-host  
 
Add-SPSolution $SolutionPath | Out-Null   

write-host "[[STEP]] Added Solution $SolutionName successfully." -foregroundcolor Green
write-host 

write-host "[[STEP]] Deploying solution: $SolutionName." -foregroundcolor Yellow
write-host
$Solution = Get-SPSolution | ? {($_.Name -eq $SolutionName) -and ($_.Deployed -eq $false)}   
if(($Solution -ne $null) -and ($Solution.ContainsWebApplicationResource))   
{   
	Install-SPSolution $SolutionName -AllWebApplications -GACDeployment -Confirm:$false   
}   
else   
{   
	Install-SPSolution $SolutionName -GACDeployment -Confirm:$false   
}   
while ($Solution.Deployed -eq $false)   
{   
	Start-Sleep 5   
} 

write-host "[[STEP]] Solution WSP is deployed successfully." -foregroundcolor Green
write-host 

if (-not $WasAdminServiceRunning)   
{   
	write-host "[[STEP]] Stopping SharePoint Administration Service." -foregroundcolor Yellow
	write-host
    Stop-Service $AdminServiceName  
	write-host "[[STEP]] SharePoint Administration Service Stopped." -foregroundcolor Green
	write-host
}  

